package com.freak.neteasecloudmusic.modules.video.fragment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.modules.video.entity.VideoEntity;
import com.freak.neteasecloudmusic.utils.StringUtils;
import com.freak.neteasecloudmusic.view.video.JzViewOutlineProvider;
import com.freak.neteasecloudmusic.view.video.MyJzVideoPlay;

import java.util.List;

import cn.jzvd.Jzvd;

/**
 * Created by Freak on 2020/3/8.
 */
public class VideoAdapter extends BaseQuickAdapter<VideoEntity.DatasBean, BaseViewHolder> {
    private Context mContext;


    public VideoAdapter(int layoutResId, @Nullable List<VideoEntity.DatasBean> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, VideoEntity.DatasBean item) {
        if (item.getData().getUrlInfo() != null) {
            MyJzVideoPlay myJzVideoPlay = helper.getView(R.id.videoTabItemJzVideo);
            //设置视频地址
            myJzVideoPlay.setUp(item.getData().getUrlInfo().getUrl(), "");
            //设置视频圆角 如果视频显示不全，可能就是由于设置圆角导致的
            myJzVideoPlay.setOutlineProvider(new JzViewOutlineProvider(15));
            myJzVideoPlay.setClipToOutline(true);
            Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
            myJzVideoPlay.setClickUi(new MyJzVideoPlay.ClickUi() {
                @Override
                public void onClickUiToggle() {
                    helper.getView(R.id.textViewVideoCount).setVisibility(View.VISIBLE);
                    helper.getView(R.id.textViewVideoTime).setVisibility(View.VISIBLE);
                    helper.getView(R.id.viewVideoTime).setVisibility(View.VISIBLE);
                    helper.getView(R.id.viewVideo).setVisibility(View.VISIBLE);
                }

                @Override
                public void onClickStart() {
                    helper.getView(R.id.textViewVideoCount).setVisibility(View.GONE);
                    helper.getView(R.id.textViewVideoTime).setVisibility(View.GONE);
                    helper.getView(R.id.viewVideoTime).setVisibility(View.GONE);
                    helper.getView(R.id.viewVideo).setVisibility(View.GONE);
                }
            });
            //设置视频略缩图
            Glide.with(mContext).load(item.getData().getCoverUrl()).thumbnail(0.1f).into(myJzVideoPlay.thumbImageView);

        }

        if (item.getData().getCreator() != null) {
            Glide.with(mContext).load(item.getData().getCreator().getAvatarUrl()).into((ImageView) helper.getView(R.id.circularBeadClipPathImageViewPhoto));
            Glide.with(mContext).load(item.getData().getCreator().getBackgroundUrl()).into((ImageView) helper.getView(R.id.circularBeadClipPathImageViewBg));
            helper.setText(R.id.textViewVideoAuthor, item.getData().getCreator().getNickname());
        } else if (item.getData().getArtists() != null && item.getData().getArtists().size() > 0) {
            Glide.with(mContext).load(item.getData().getCoverUrl()).into((ImageView) helper.getView(R.id.circularBeadClipPathImageViewPhoto));
            Glide.with(mContext).load(item.getData().getArtists().get(0).getImg1v1Url()).into((ImageView) helper.getView(R.id.circularBeadClipPathImageViewBg));
            helper.setText(R.id.textViewVideoAuthor, item.getData().getArtists().get(0).getName());
        }
        helper.setText(R.id.textViewCommentCount, String.valueOf(item.getData().getCommentCount()));
        helper.setText(R.id.textViewZanCount, String.valueOf(item.getData().getPraisedCount()));

        helper.setText(R.id.textViewVideoTime, StringUtils.stringForTime((int) item.getData().getDurationms()));
        helper.setText(R.id.textViewVideoCount, TextUtils.isEmpty(item.getData().getPlayTime()) ? String.valueOf(item.getData().getPlayCount()) : item.getData().getPlayTime());
        helper.setText(R.id.textViewVideoContext, TextUtils.isEmpty(item.getData().getTitle()) ? item.getData().getName() : item.getData().getTitle());
        if (item.getData().getVideoGroup() != null && item.getData().getVideoGroup().size() > 0) {
            helper.setText(R.id.textViewVideoTip, item.getData().getVideoGroup().get(0).getName());
        }
    }
}
